/*------------------------------------------------------------------------------
 * Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805: 
 * A Comprehensive OCPJP 7 Certification Guide
 * by SG Ganesh and Tushar Sharma
------------------------------------------------------------------------------*/
package chapter10.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;

// The class attempts to acquire a connection with the database
class DbConnect {
	public static void main(String[] args) {
		// url points to jdbc protocol : mysql subprotocol; localhost is the
		// address
		// of the server where we installed our DBMS (i.e. on local machine) and
		// 3306 is the port on which we need to contact our DBMS
		String url = "jdbc:mysql://localhost:3306/";
		// we are connecting to the addressBook database we created earlier
		String database = "addressBook";
		// we login as "root" user with password "mysql123"
		String userName = "root";
		String password = "mysql123";// pwd final
		try (Connection connection = DriverManager.getConnection(
				url + database, userName, password)) {
			System.out.println("Database connection: Successful");
		} catch (Exception e) {
			System.out.println("Database connection: Failed");
			e.printStackTrace();
			/**
			 * Database connection: Failed java.sql.SQLException: No suitable
			 * driver found for jdbc:mysql://localhost:3306/addressBook at
			 * java.sql.DriverManager.getConnection(DriverManager.java:596) at
			 * java.sql.DriverManager.getConnection(DriverManager.java:215) at
			 * chapter10.JDBC.DbConnect.main(DbConnect.java:24)
			 */

			/**
			 * SOLUCIÓN Windows<br>
			 * <br>
			 * WIndows : Copy mysql-connector.jar to C:\Program
			 * Files\Java\jdk1.6.0\jre\lib\ext and copy the same file to
			 * C:\Program Files\Java\jre1.6.0\lib\ext
			 * 
			 * go to My Computer -> Properties -> Advanced -> Environment
			 * Variables
			 * 
			 * Set these paths
			 * 
			 * JAVA_HOME C:\Program Files\Java\jdk1.6.0
			 * 
			 * PATH C:\Program Files\Java\jdk1.6.0\bin;
			 * 
			 * CLASSPATH .;C:\Program
			 * Files\Java\jre1.6.0\lib\ext\mysql-connector.jar;.;
			 */

			/**
			 * SOLUCIÓN MAC Copiar el .jar (mysql-connector-java-5.1.34-bin) en
			 * /Library/Java/Extensions
			 */
		}
	}
}
